package com.scalar.chiptrack.finance.proformainvoice.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.release.ReleaseHelper;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.finance.proformainvoice.forms.ProformaInvoiceForm;
import com.scalar.chiptrack.finance.proformainvoice.dao.ProformaInvoiceDAO;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * User: KP
 * Date: Sep 28, 2006
 * Time: 3:10:16 PM
 */
public class ProformaInvoiceSearchCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/newproformaInvoice.do";
    
    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        ProformaInvoiceForm invoiceForm     = ( ProformaInvoiceForm ) form;
        ActionForward actionForward         = mapping.findForward( "search_invoices" );
        String searchWithCompulsoryFields   = request.getParameter("searchWithCompulsoryFields");
        HttpSession mSession                = request.getSession();
        User user                           = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );
        HashMap errors                      = new HashMap();
        HashMap info                        = new HashMap();
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return ( mapping.findForward ( "login" ) );
        }

        try
        {
            if( "true".equalsIgnoreCase( searchWithCompulsoryFields ) )
            {
                ArrayList profromaInvoiceList = ProformaInvoiceDAO.loadProformaInvoiceList( null, invoiceForm );

                if( ( profromaInvoiceList == null ) || ( profromaInvoiceList.size() == 0 ) )
                {
                    info.put( "Profroma Invoice matching the given search criteria could not be found", null );
                }
                else
                {
                    request.setAttribute( "profromaInvoiceList", profromaInvoiceList );
                }
            }
            else
            {
                Calendar calendar = Calendar.getInstance();
                invoiceForm.setSearchYear( ( calendar.get( Calendar.YEAR ) ) + "" );
            }
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            Logger.error( "Error while searching for Proforma Invoice", e );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, "Error while searching for Proforma Invoice" );

            mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while searching for Proforma Invoice", e ) );
        }

        request.setAttribute( "yearList", ReleaseHelper.getYearList() );
        request.setAttribute( "partNumberList", NewProductDAO.loadPartNumbers (null, 1, Constants.LOCATION_TYPE_FG, true, true, true, 0 ) );
        request.setAttribute( "salesOrderList", ProformaInvoiceDAO.loadApprovedSalesOrderList( null ) );
        request.setAttribute( "customerList", CustomerDAO.getAllCustomerNamesByUserType( null, user ) );

        return actionForward;
    }
}
